/**
 * 
 */
package com.siteseat.common.entity;

import java.io.IOException;
import java.util.Map;

import com.siteseat.common.exception.InternalException;
import com.siteseat.common.json.JacksonMapper;

/**
 *
 */
public abstract class Entity implements java.io.Serializable {

  public enum ValidateLevel {
    LOW( 0 ), MIDDLE( 1 ), HIGH( 2 );

    public final int number;

    private ValidateLevel( int number ) {
      this.number = number;
    }
  }

  /**
   * 
   */
  private static final long serialVersionUID = 8264894833649828124L;

  /**
   * 
   */
  public abstract void validate( ValidateLevel level );

  /**
   * 
   */
  public static <T extends Entity> void validate( T[] entities, ValidateLevel level, Object hint ) {
    for( int i = 0; i < entities.length; i++ ) {
      validate( entities[i], level, hint + " " + i );
    }
  }

  /**
   * 
   */
  public static <T extends Entity> void validate( Map<String, T> entities, ValidateLevel level, Object hint ) {
    for( Map.Entry<String, T> entry: entities.entrySet() ) {
      validate( entry.getValue(), level, hint + " " + entry.getKey() );
    }
  }

  /**
   */
  public static <T extends Entity> void validate( T entity, ValidateLevel level, Object hint ) {
    if( entity == null ) {
      throw new InternalException( "object is null: " + hint );
    }
    entity.validate( level );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String toString() {
    try {
      return JacksonMapper.MAPPER.defaultPrettyPrintingWriter().writeValueAsString( this );
    } catch( IOException e ) {
      throw new InternalException( e );
    }
  }

}
